// components/mEditor/mEditor.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    data: {
        formats: {},
        readOnly: false,
        placeholder: '',
        editorHeight: 300,
        keyboardHeight: 0,
        isIOS: false,
        isfocus: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        focusHandle() {
            this.setData({
                isfocus: true
            });
        },
        blurHandle() {
            this.setData({
                isfocus: false
            });
        },
        ready() {
            const platform = wx.getSystemInfoSync().platform
            const isIOS = platform === 'ios'
            this.setData({
                isIOS
            })
            const that = this
            this.updatePosition(0)
            let keyboardHeight = 0
            wx.onKeyboardHeightChange(res => {
                if (res.height === keyboardHeight) return
                const duration = res.height > 0 ? res.duration * 1000 : 0
                keyboardHeight = res.height
                setTimeout(() => {
                    that.updatePosition(keyboardHeight)
                    that.editorCtx.scrollIntoView()
                }, duration)
    
            })
        },
        readOnlyChange() {
            this.setData({
                readOnly: !this.data.readOnly
            })
        },
        updatePosition(keyboardHeight) {
            const toolbarHeight = 50
            const {
                windowHeight,
                platform
            } = wx.getSystemInfoSync()
            let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
            this.setData({
                editorHeight,
                keyboardHeight
            })
        },
        calNavigationBarAndStatusBar() {
            const systemInfo = wx.getSystemInfoSync()
            const {
                statusBarHeight,
                platform
            } = systemInfo
            const isIOS = platform === 'ios'
            const navigationBarHeight = isIOS ? 44 : 48
            return statusBarHeight + navigationBarHeight
        },

        setContents(html) {
            this.editorCtx.setContents({
                html: html
            });
        },

        onEditorReady() {
            // 富文本节点渲染完成
            const query = wx.createSelectorQuery().in(this);
            query
                .select('#editor')
                .context(res => {
                    this.editorCtx = res.context;
                    this.ready();
                    this.triggerEvent('ready',this.editorCtx)
                    this.editorCtx.setContents({
                        html: this.data.html
                    });
                })
                .exec(this);
        },
        blur() {
            this.editorCtx.blur()
        },
        format(e) {
            let {
                name,
                value
            } = e.target.dataset
            if (!name) return
            // console.log('format', name, value)
            this.editorCtx.format(name, value)

        },
        onStatusChange(e) {
            const formats = e.detail
            this.setData({
                formats
            })
        },
        inputHandle(e) {
            this.triggerEvent('input',e);
        },
        insertDivider() {
            this.editorCtx.insertDivider({
                success: function () {
                    console.log('insert divider success')
                }
            })
        },
        clear() {
            this.editorCtx.clear({
                success: function (res) {
                    console.log("clear success")
                }
            })
        },
        removeFormat() {
            this.editorCtx.removeFormat()
        },
        insertDate() {
            const date = new Date()
            const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
            this.editorCtx.insertText({
                text: formatDate
            })
        },
        insertImage() {
            const that = this
            wx.chooseImage({
                count: 1,
                success: function (res) {

                  var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };
                  wx.uploadFile({
                    url: app.globalData.uploadUrl, 
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    header:header,
                    formData: { user: 'test' },
                    success(res) {
                      var api_data = JSON.parse(res.data) 
                      // 上传完成需要更新 fileList
                      that.editorCtx.insertImage({
                        src: app.globalData.siteUrl + api_data.data.pic,
                        data: {
                            id: 'abcd',
                            role: 'god'
                        },
                        width: '80%',
                        success: function () {
                            console.log('insert image success')
                        }
                    })
                      //form.pic1 = api_data.data.pic;
                    },
                  });

                    
                }
            })
        }

    }
})